/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.cd;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import who.tmap.bo.ThematicMap;
import who.tmap.bo.cd.CDProportionalSymbols;
import who.tmap.bo.cd.CDShapes;
import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.bo.cd.impl.CartographicDisplayFactory;
import who.tmap.common.TMapException;
import who.tmap.toolkit.shape.annotation.impl.ImageHelper;
import who.tmap.ui.util.JComboBoxColor;
import who.tmap.ui.util.MiscUIHelper;
import who.tmap.util.MiscHelper;

/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class JPanelCDProportionalSymbols extends JPanel 
										 implements PanelCD {

    private static Logger logger = Logger.getLogger(JPanelCDProportionalSymbols.class); 		
	
	private static final int DEFAULT_SCALING_FACTOR =10;
    public static final String DEFAULT_BITMAP ="ic_tmap.gif";
		
	private JPanel thisPanel =this;
	private ThematicMap thematicMap =null;  
	private String joinIdColumnName =null;
	private String dataColumnName =null;
	private boolean dirty =false;
	
	private JPanelCDHelper jPanelCDHelper =new JPanelCDHelper();  

	private JComboBox jComboBoxJoinIdColumn = null;	
	private JComboBox jComboBoxDataColumn = null;
	private JComboBox jComboBoxShape = null;
	private JTextField jTextFieldScalingFactor = null;
	private JComboBox jComboBoxFillPattern = null;
	private JComboBox jComboBoxColor = null;

	private JPanel jPanelInfo = null;
	private MiscUIHelper miscUIHelper =new MiscUIHelper();
	
	private ImageHelper imageHelper =new ImageHelper();
	private boolean initializing =false;

	/**
	 * This is the default constructor
	 */
	public JPanelCDProportionalSymbols() {
		super();
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		initializing =true;
		setLayout(new BorderLayout());
		setSize(300, 200);
		add(getJPanelInfo(), BorderLayout.NORTH);
		add( Box.createVerticalGlue(), BorderLayout.CENTER );
		
		imageHelper.setImage( new MiscHelper().getImageIcon( DEFAULT_BITMAP ).getImage() );
		
		initializing =false;
	}
	
	/**
	 * This method initializes jComboBoxData
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxDataColumn() {
		if(jComboBoxDataColumn == null) {
			jComboBoxDataColumn = new JComboBox();
			jComboBoxDataColumn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxDataColumn;
	}
	
	/**
	 * This method initializes jComboBoxData
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxJoinIdColumn() {
		if(jComboBoxJoinIdColumn == null) {
			jComboBoxJoinIdColumn = new JComboBox();
			jComboBoxJoinIdColumn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});

		}
		return jComboBoxJoinIdColumn;
	}
	
	/**
	 * This method initializes jComboBoxShape
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxShape() {
		if(jComboBoxShape == null) {
			jComboBoxShape = new JComboBox( CDShapes.names );
			jComboBoxShape.setSelectedIndex( CDShapes.DEFAULT );
			jComboBoxShape.addActionListener( new ActionListener() {
				public void actionPerformed( ActionEvent e ) {
					setDirty( true );
					
					if ( jComboBoxShape.getSelectedIndex() == CDShapes.BITMAP && !initializing ) {
						
						File file =miscUIHelper.openBitmapFile();
						if ( file != null ) {
					    	try {
								imageHelper.loadFile( file );
								setDirty( true );
							} catch (IOException e1) {
								logger.error( e1 );
							}
						}

					}
				}
			});
		}
		return jComboBoxShape;
	}
	
	/**
	 * This method initializes jTextFieldScalingFactor
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldScalingFactor() {
		if(jTextFieldScalingFactor == null) {
			jTextFieldScalingFactor = new JTextField();
			jTextFieldScalingFactor.setText( String.valueOf( DEFAULT_SCALING_FACTOR ) );
			jTextFieldScalingFactor.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent e) {
					setDirty( true );				
				}
			});
		}
		return jTextFieldScalingFactor;
	}
	
	/**
	 * This method initializes jComboBoxFillPattern
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxFillPattern() {
		if(jComboBoxFillPattern == null) {
			jComboBoxFillPattern = new JComboBoxPattern();
			jComboBoxFillPattern.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxFillPattern;
	}
	
	/**
	 * This method initializes jComboBoxColor
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxColor() {
		if(jComboBoxColor == null) {
			jComboBoxColor = new JComboBoxColor();
			jComboBoxColor.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxColor;
	}
		
	public ThematicMap getThematicMap() {
		return thematicMap;
	}
	
	public void setThematicMap(ThematicMap thematicMap) {
		this.thematicMap = thematicMap;
	}
	
	public void refreshAttrColumnNames() {
		jPanelCDHelper.populateJComboBox( thematicMap, getJComboBoxJoinIdColumn() );
		jPanelCDHelper.populateJComboBox( thematicMap, getJComboBoxDataColumn() );	

		jPanelCDHelper.selectDefaultJoinIdAndDataColumns( getJComboBoxJoinIdColumn(), getJComboBoxDataColumn() );
	}
	
	public void refreshData() {
		if ( thematicMap.getCartographicDisplay() instanceof CDProportionalSymbols ) {
			CDProportionalSymbols ps =(CDProportionalSymbols)thematicMap.getCartographicDisplay();
			
			if ( ps != null ) {
				getJComboBoxJoinIdColumn().setSelectedItem( ps.getJoinIdColumnName() );
				getJComboBoxDataColumn().setSelectedItem( ps.getDataColumnName() );
				initializing =true;
				try {
					getJComboBoxShape().setSelectedItem( CDShapes.names[ ps.getShape() ] );
				} finally {
					initializing =false;
				}
				
				if ( ps.getShape() == CDShapes.BITMAP ) {
					imageHelper.setImage( ps.getImage() );
					imageHelper.setImageBytes( ps.getImageBytes() );
				}
				
				getJTextFieldScalingFactor().setText( Integer.toString( ps.getScalingFactor() ) );
				((JComboBoxColor)getJComboBoxColor()).setSelectedColor( ps.getColor() );
				
				setDirty( ps.isDirty() );
			}
		}
	}	
	
	public CartographicDisplay getCartographicDisplay() throws TMapException {
		CDProportionalSymbols cd =(CDProportionalSymbols)CartographicDisplayFactory.getInstance().get( CDTypes.PROPORTIONAL_SYMBOLS );
		
		cd.setShape( getJComboBoxShape().getSelectedIndex() );
		
		if ( cd.getShape() == CDShapes.BITMAP ) {
			try {
				cd.setImage( imageHelper.getImage() );
				cd.setImageBytes( imageHelper.getImageBytes() );
			} catch (Exception e) {
				logger.error( e );
			}
		}
		
		cd.setColor( ((JComboBoxColor)getJComboBoxColor()).getSelectedColor() );
		cd.setPolygonAssociatedData( null );
		cd.setDataColumnName( (String)getJComboBoxDataColumn().getSelectedItem() );
		cd.setJoinIdColumnName( (String)getJComboBoxJoinIdColumn().getSelectedItem() );
		cd.setFillPattern( getJComboBoxFillPattern().getSelectedIndex() );
		
		String scalingFactor =getJTextFieldScalingFactor().getText();
		try {
			cd.setScalingFactor( Integer.parseInt( scalingFactor ) );
		} catch( Exception e ) {
			cd.setScalingFactor( DEFAULT_SCALING_FACTOR );
			getJTextFieldScalingFactor().setText( String.valueOf( cd.getScalingFactor() ) );
		}
		
		cd.setDirty( isDirty() );
		
		return cd;
	}

	/**
	 * This method initializes jPanelInfo	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelInfo() {
		if (jPanelInfo == null) {
			
			GridLayout gridLayout = new GridLayout(6, 2, 5, 5);
			jPanelInfo = new JPanel();
			jPanelInfo.setLayout(gridLayout);
			
			jPanelInfo.add( new JLabel( "Join ID column" ), null );
			jPanelInfo.add(getJComboBoxJoinIdColumn(), null);
			
			jPanelInfo.add( new JLabel( "Data column" ), null);
			jPanelInfo.add(getJComboBoxDataColumn(), null);
			
			jPanelInfo.add( new JLabel( "Shape" ), null);
			jPanelInfo.add(getJComboBoxShape(), null);
			
			jPanelInfo.add( new JLabel( "Scaling factor" ), null);
			jPanelInfo.add(getJTextFieldScalingFactor(), null);
			
			jPanelInfo.add( new JLabel( "Fill pattern" ), null);
			jPanelInfo.add(getJComboBoxFillPattern(), null);
			
			jPanelInfo.add( new JLabel( "Color" ), null);
			jPanelInfo.add(getJComboBoxColor(), null);
		}
		return jPanelInfo;
	}

	public boolean isDirty() {
		return dirty;
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
	
	public String getDataColumnName() {
		return (String)getJComboBoxDataColumn().getSelectedItem();
	}
	
	public String getJoinIdColumnName() {
		return (String)getJComboBoxJoinIdColumn().getSelectedItem();
	}
	
	public void setDataColumnName(String dataColumnName) {
		getJComboBoxDataColumn().setSelectedItem( dataColumnName );
	}

	public void setJoinIdColumnName(String joinIdColumnName) {
		getJComboBoxJoinIdColumn().setSelectedItem( joinIdColumnName );
	}		
		
}
